package com.metaverse.backend.utils;


import org.apache.commons.beanutils.BeanUtilsBean;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;

public class ObjUtils {
    public static void merge(Object dst, Object src) {
        Objects.requireNonNull(src);
        Objects.requireNonNull(dst);
        if (!dst.getClass().equals(src.getClass())) {
            throw new RuntimeException("cannot merge different class");
        }
        BeanUtilsBean notNull = new NullAwareBeanUtilsBean();
        try {
            notNull.copyProperties(dst, src);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static String[] getFields(Class clazz) {
        Field[] fields = clazz.getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }


}
